package mweb

import (
	"fmt"
	"gitee.com/dennis-mxx/mxx-core/mlogger"
	"gitee.com/dennis-mxx/mxx-core/msystem"
	"runtime"
	"strings"
)

var RUN = animation{len: 80, str: `*`}

type animation struct {
	len int
	str string
}

func (domain *animation) ShowDefault() {
	data := make(map[string]interface{})
	data["environment"] = msystem.Environment
	data["module"] = msystem.Iris.ApplicationName
	data["port"] = msystem.Iris.Port
	data["debug"] = msystem.Log.Level
	data["version"] = runtime.Version()

	domain.Show([]string{"module", "port", "debug", "version", "environment"}, data)
}

func (domain *animation) Show(keys []string, data map[string]interface{}) {
	domain.printAll()
	domain.printNullLine()
	for _, key := range keys {
		domain.printKeyVal(key, data[key])
	}
	domain.printNullLine()
	domain.printAll()
}

func (domain *animation) printNullLine() {
	build := strings.Builder{}
	build.WriteString(domain.str)
	for i := 2; i < domain.len; i++ {
		build.WriteString(" ")
	}
	build.WriteString(domain.str)
	mlogger.Logger.Info(build.String())

}
func (domain *animation) printKeyVal(key string, val interface{}) {
	printStr := fmt.Sprintf("%v           %v : %v", domain.str, key, val)
	len := len(printStr)
	builder := strings.Builder{}
	builder.WriteString(printStr)
	for ; len < domain.len-1; len++ {
		builder.WriteString(" ")
	}
	builder.WriteString(domain.str)
	mlogger.Logger.Info(builder.String())

}
func (domain *animation) printAll() {
	buff := strings.Builder{}
	for i := 0; i < domain.len; i++ {
		buff.WriteString(domain.str)
	}
	mlogger.Logger.Info(buff.String())
}
